package com.smm.web.vmc.controller.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smm.web.vmc.domain.Receipt;
import com.smm.web.vmc.domain.User;
import com.smm.web.vmc.domain.relation.UserReceiptRelation;
import com.smm.web.vmc.dto.JsonReturn;
import com.smm.web.vmc.dto.UserDto;
import com.smm.web.vmc.service.ReceiptService;
import com.smm.web.vmc.service.UserReceiptRelationService;
import com.smm.web.vmc.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zhenghao on 2016/9/6.
 *
 */
@Controller
@RequestMapping("/admin/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private ReceiptService receiptService;

    @Resource
    private UserReceiptRelationService userReceiptRelationService;

    @RequestMapping("/list.do")
    public ModelAndView list(){

        //获取所有用户信息
        List<UserDto> userDtoList = userService.getAllForDto();

        ModelAndView mv = new ModelAndView("admin/user/list");

        mv.addObject("userList",userDtoList);

        return mv;
    }


    @RequestMapping("/choiseReceipt.do")
    public ModelAndView choiseReceipt(int userId){

        //获取用户信息
        User user = userService.getOneById(userId);

        //获取所有仓单列表
        List<Receipt> receipts = receiptService.getAll();

        //获取该用户已分配的仓单列表
        List<Receipt> userReceipts = userReceiptRelationService.getReceiptByUser(userId);

        List<String> userRepNos = new ArrayList<>();

        if(userReceipts!=null && userReceipts.size()>0){

            userRepNos = userReceipts.stream().map(Receipt::getNo).collect(Collectors.toList());
        }


        ModelAndView mv = new ModelAndView("admin/user/choiseReceipt");

        mv.addObject("user",user);
        mv.addObject("receipts",receipts);
        mv.addObject("userRepNos",userRepNos);

        return mv;
    }




    @RequestMapping("/subChoiseRep.do")
    @ResponseBody
    public JsonReturn subChoiseRep(String jsonStr){

        try {

            //解析json 数据
            JSONObject obj = JSON.parseObject(jsonStr);

            int userId=Integer.parseInt(obj.get("userId").toString());

            JSONArray noArray = obj.getJSONArray("repNo");

            List<String> repNos = new ArrayList<>();

            for(Object no : noArray){

                repNos.add(no.toString());

            }

            //调用更新方法
            userService.choiseReceipt(userId,repNos);

        } catch (Exception e) {
            e.printStackTrace();

            return new JsonReturn(0,"分配失败！"+e.getMessage());

        }

        return new JsonReturn(1,"分配成功");

    }


    //修改用户手机号码
    @RequestMapping("/updatePhone.do")
    @ResponseBody
    public JsonReturn updatePhone(int userId,String phone){

        try{

            this.userService.updateUserPhone(userId,phone);

            return new JsonReturn(1,"修改成功");


        }catch (Exception e){

            e.printStackTrace();

            return new JsonReturn(0,"修改失败"+e.getMessage());

        }
    }


}
